package com.ai.config;

import com.ai.handler.MyMetaObjectHandler;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan(basePackages = "com.ai.mapper")
public class MybatisPlusConfiguration {

    /**
     * Mp主拦截器
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor());   // 添加分页拦截器
        mybatisPlusInterceptor.addInnerInterceptor(optimisticLockerInnerInterceptor()); // 添加乐观锁拦截器
        return mybatisPlusInterceptor;
    }

    /**
     * 分页拦截器
     *      setDbType  类型为DbType  设置数据库类型
     *      setMaxLimit  设置单次分页的最大数量
     *      setOverflow  分页溢出后是否进行自动截断  默认false
     */
    public PaginationInnerInterceptor paginationInnerInterceptor(){
        PaginationInnerInterceptor pages = new PaginationInnerInterceptor();
        pages.setDbType(DbType.MYSQL);  // 指定数据源为 Mysql
        pages.setMaxLimit(1000L);   // 指定单次分页的最大限制
        return pages;
    }

    /**
     * 乐观锁拦截器
     * @return
     */
    public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor(){
        return new OptimisticLockerInnerInterceptor();
    }

    /**
     * 自动填充字段
     * @return
     */
    @Bean
    public MetaObjectHandler metaObjectHandler(){
        return new MyMetaObjectHandler();
    }
}
